<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>响应性基础 API | Vue.js</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link href="https://fonts.googleapis.com/css?family=Inter:300,400,500,600|Open+Sans:400,600;display=swap" rel="stylesheet">
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="icon" href="/logo.png">
    <script src="https://player.youku.com/iframeapi"></script>
    <meta name="description" content="Vue.js - The Progressive JavaScript Framework">
    <link rel="preload" href="/assets/css/0.styles.60414370.css" as="style"><link rel="preload" href="/assets/js/app.0a9bf1a0.js" as="script"><link rel="preload" href="/assets/js/9.f307b0a5.js" as="script"><link rel="preload" href="/assets/js/18.9c9769d1.js" as="script"><link rel="preload" href="/assets/js/10.523605bb.js" as="script"><link rel="preload" href="/assets/js/37.af806c96.js" as="script"><link rel="prefetch" href="/assets/js/1.832c44bf.js"><link rel="prefetch" href="/assets/js/100.6c7a3206.js"><link rel="prefetch" href="/assets/js/101.656addb5.js"><link rel="prefetch" href="/assets/js/102.fddd4c59.js"><link rel="prefetch" href="/assets/js/103.8b570fcc.js"><link rel="prefetch" href="/assets/js/104.b1a5605a.js"><link rel="prefetch" href="/assets/js/105.edde2e45.js"><link rel="prefetch" href="/assets/js/106.3b04c318.js"><link rel="prefetch" href="/assets/js/107.98cab24c.js"><link rel="prefetch" href="/assets/js/108.15d2ab25.js"><link rel="prefetch" href="/assets/js/109.5f483cd0.js"><link rel="prefetch" href="/assets/js/11.43202c8c.js"><link rel="prefetch" href="/assets/js/110.de44b32c.js"><link rel="prefetch" href="/assets/js/111.e247818d.js"><link rel="prefetch" href="/assets/js/112.f330b33d.js"><link rel="prefetch" href="/assets/js/113.d26db724.js"><link rel="prefetch" href="/assets/js/114.4a085227.js"><link rel="prefetch" href="/assets/js/115.6f41e46c.js"><link rel="prefetch" href="/assets/js/116.d9f31fc2.js"><link rel="prefetch" href="/assets/js/117.6bc037ec.js"><link rel="prefetch" href="/assets/js/118.b6d13098.js"><link rel="prefetch" href="/assets/js/119.40081175.js"><link rel="prefetch" href="/assets/js/12.9ac43f9f.js"><link rel="prefetch" href="/assets/js/120.dc59e2a2.js"><link rel="prefetch" href="/assets/js/121.5d6c245d.js"><link rel="prefetch" href="/assets/js/122.44eb5e5a.js"><link rel="prefetch" href="/assets/js/123.2fbebc5c.js"><link rel="prefetch" href="/assets/js/124.9733dae9.js"><link rel="prefetch" href="/assets/js/125.35fe073b.js"><link rel="prefetch" href="/assets/js/126.b2d1ea91.js"><link rel="prefetch" href="/assets/js/127.3d7f3105.js"><link rel="prefetch" href="/assets/js/128.aa600ea3.js"><link rel="prefetch" href="/assets/js/129.92b123b0.js"><link rel="prefetch" href="/assets/js/13.61cfe0ac.js"><link rel="prefetch" href="/assets/js/130.65930d6d.js"><link rel="prefetch" href="/assets/js/131.fe452d41.js"><link rel="prefetch" href="/assets/js/132.8db41894.js"><link rel="prefetch" href="/assets/js/133.2422a318.js"><link rel="prefetch" href="/assets/js/134.28ae1eb3.js"><link rel="prefetch" href="/assets/js/135.854307ee.js"><link rel="prefetch" href="/assets/js/136.42e4e007.js"><link rel="prefetch" href="/assets/js/137.c557b795.js"><link rel="prefetch" href="/assets/js/138.8314e9f0.js"><link rel="prefetch" href="/assets/js/139.4a91ed8c.js"><link rel="prefetch" href="/assets/js/14.c6ae250c.js"><link rel="prefetch" href="/assets/js/140.5be2f667.js"><link rel="prefetch" href="/assets/js/141.02fbd174.js"><link rel="prefetch" href="/assets/js/142.f1c4e87e.js"><link rel="prefetch" href="/assets/js/143.59791aa7.js"><link rel="prefetch" href="/assets/js/144.fb6601d0.js"><link rel="prefetch" href="/assets/js/145.11777ee6.js"><link rel="prefetch" href="/assets/js/15.99a67fb3.js"><link rel="prefetch" href="/assets/js/16.54274551.js"><link rel="prefetch" href="/assets/js/17.620b927c.js"><link rel="prefetch" href="/assets/js/19.3a6eb738.js"><link rel="prefetch" href="/assets/js/2.10e2fb64.js"><link rel="prefetch" href="/assets/js/20.4f240fd5.js"><link rel="prefetch" href="/assets/js/21.b5c24ec0.js"><link rel="prefetch" href="/assets/js/22.6b36d53a.js"><link rel="prefetch" href="/assets/js/23.0742dc98.js"><link rel="prefetch" href="/assets/js/24.490994ab.js"><link rel="prefetch" href="/assets/js/25.854225c9.js"><link rel="prefetch" href="/assets/js/26.cb8bede1.js"><link rel="prefetch" href="/assets/js/27.01fe0de5.js"><link rel="prefetch" href="/assets/js/28.8d33f619.js"><link rel="prefetch" href="/assets/js/29.1005b103.js"><link rel="prefetch" href="/assets/js/3.4ba5caef.js"><link rel="prefetch" href="/assets/js/30.0629b722.js"><link rel="prefetch" href="/assets/js/31.41c2c5b0.js"><link rel="prefetch" href="/assets/js/32.7709b53d.js"><link rel="prefetch" href="/assets/js/33.7e973829.js"><link rel="prefetch" href="/assets/js/34.107475bf.js"><link rel="prefetch" href="/assets/js/35.e83bdd9b.js"><link rel="prefetch" href="/assets/js/36.e0922c67.js"><link rel="prefetch" href="/assets/js/38.94bae313.js"><link rel="prefetch" href="/assets/js/39.f15cd956.js"><link rel="prefetch" href="/assets/js/4.b65b2ec4.js"><link rel="prefetch" href="/assets/js/40.18818521.js"><link rel="prefetch" href="/assets/js/41.cc39555d.js"><link rel="prefetch" href="/assets/js/42.eb469d2e.js"><link rel="prefetch" href="/assets/js/43.7bdc7f53.js"><link rel="prefetch" href="/assets/js/44.c15154a9.js"><link rel="prefetch" href="/assets/js/45.e28b1e26.js"><link rel="prefetch" href="/assets/js/46.1a582225.js"><link rel="prefetch" href="/assets/js/47.1681e4e7.js"><link rel="prefetch" href="/assets/js/48.c7e4df2a.js"><link rel="prefetch" href="/assets/js/49.24a40e32.js"><link rel="prefetch" href="/assets/js/5.436c63ff.js"><link rel="prefetch" href="/assets/js/50.79ca1e35.js"><link rel="prefetch" href="/assets/js/51.4d6124d9.js"><link rel="prefetch" href="/assets/js/52.93b75fa3.js"><link rel="prefetch" href="/assets/js/53.99ab0219.js"><link rel="prefetch" href="/assets/js/54.91605e00.js"><link rel="prefetch" href="/assets/js/55.d1fdbf6f.js"><link rel="prefetch" href="/assets/js/56.e162390f.js"><link rel="prefetch" href="/assets/js/57.af6ab199.js"><link rel="prefetch" href="/assets/js/58.71eff2a5.js"><link rel="prefetch" href="/assets/js/59.b70c19c9.js"><link rel="prefetch" href="/assets/js/6.1bc8dee2.js"><link rel="prefetch" href="/assets/js/60.03187705.js"><link rel="prefetch" href="/assets/js/61.c9596d10.js"><link rel="prefetch" href="/assets/js/62.28b9a2e8.js"><link rel="prefetch" href="/assets/js/63.a5f17acf.js"><link rel="prefetch" href="/assets/js/64.deca5d49.js"><link rel="prefetch" href="/assets/js/65.8014765f.js"><link rel="prefetch" href="/assets/js/66.c136f2b4.js"><link rel="prefetch" href="/assets/js/67.55b8c4d6.js"><link rel="prefetch" href="/assets/js/68.ff568b2f.js"><link rel="prefetch" href="/assets/js/69.cbb0de95.js"><link rel="prefetch" href="/assets/js/70.9a450c62.js"><link rel="prefetch" href="/assets/js/71.56ddc8d7.js"><link rel="prefetch" href="/assets/js/72.ff8c82c4.js"><link rel="prefetch" href="/assets/js/73.5a3ae71c.js"><link rel="prefetch" href="/assets/js/74.d43f66e7.js"><link rel="prefetch" href="/assets/js/75.a0490d28.js"><link rel="prefetch" href="/assets/js/76.51277fc3.js"><link rel="prefetch" href="/assets/js/77.12725190.js"><link rel="prefetch" href="/assets/js/78.e3426443.js"><link rel="prefetch" href="/assets/js/79.34f80eb5.js"><link rel="prefetch" href="/assets/js/80.e7d07c97.js"><link rel="prefetch" href="/assets/js/81.497c1c98.js"><link rel="prefetch" href="/assets/js/82.5edfa1e6.js"><link rel="prefetch" href="/assets/js/83.72c9f367.js"><link rel="prefetch" href="/assets/js/84.72494822.js"><link rel="prefetch" href="/assets/js/85.02c708f2.js"><link rel="prefetch" href="/assets/js/86.75fdd91f.js"><link rel="prefetch" href="/assets/js/87.431ab614.js"><link rel="prefetch" href="/assets/js/88.06c97259.js"><link rel="prefetch" href="/assets/js/89.1c6506f7.js"><link rel="prefetch" href="/assets/js/90.7f074177.js"><link rel="prefetch" href="/assets/js/91.82276ba6.js"><link rel="prefetch" href="/assets/js/92.2d5fa7b8.js"><link rel="prefetch" href="/assets/js/93.9ae1367c.js"><link rel="prefetch" href="/assets/js/94.3ba6340b.js"><link rel="prefetch" href="/assets/js/95.e31bf445.js"><link rel="prefetch" href="/assets/js/96.b6f4c3e4.js"><link rel="prefetch" href="/assets/js/97.fd612974.js"><link rel="prefetch" href="/assets/js/98.fc2e9816.js"><link rel="prefetch" href="/assets/js/99.ea1b3d86.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.a100821a.js">
    <link rel="stylesheet" href="/assets/css/0.styles.60414370.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><!----> <header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Vue.js" class="logo"> <span class="site-name can-hide">Vue.js</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="/guide/migration/introduction.html" class="nav-link">
  v3 迁移指南
</a></li><li class="dropdown-item"><!----> <a href="/style-guide/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="/cookbook/" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="/examples/markdown.html" class="nav-link">
  示例
</a></li></ul></div></div><div class="nav-item"><a href="/api/application-config.html" class="nav-link">
  API Reference
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/community/team/" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="/community/partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="/community/join/" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="/community/themes/" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://next.router.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuex.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://cli.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuejs.github.io/vue-test-utils-next-docs/guide/introduction.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://news.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/support-vuejs/#one-time-donations" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="/support-vuejs/#recurring-pledges" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://vue.threadless.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://v3.vuejs.org" target="_blank" rel="noopener noreferrer" class="nav-link external">
  English
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/vuejs/docs-next-zh-cn" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <!----></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/guide/introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="/guide/migration/introduction.html" class="nav-link">
  v3 迁移指南
</a></li><li class="dropdown-item"><!----> <a href="/style-guide/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="/cookbook/" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="/examples/markdown.html" class="nav-link">
  示例
</a></li></ul></div></div><div class="nav-item"><a href="/api/application-config.html" class="nav-link">
  API Reference
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/community/team/" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="/community/partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="/community/join/" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="/community/themes/" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://next.router.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuex.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://cli.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://vuejs.github.io/vue-test-utils-next-docs/guide/introduction.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://github.com/vuejs/vue-devtools" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://news.vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/support-vuejs/#one-time-donations" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="/support-vuejs/#recurring-pledges" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://osholic.com/?utm_source=vue&amp;utm_medium=dropdown" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://vue.threadless.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://v3.vuejs.org" target="_blank" rel="noopener noreferrer" class="nav-link external">
  English
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/vuejs/docs-next-zh-cn" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><a href="/api/application-config.html" class="sidebar-link">应用配置</a></li><li><a href="/api/application-api.html" class="sidebar-link">应用 API</a></li><li><a href="/api/global-api.html" class="sidebar-link">全局 API</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>选项</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/api/options-data.html" class="sidebar-link">Data</a></li><li><a href="/api/options-dom.html" class="sidebar-link">DOM</a></li><li><a href="/api/options-lifecycle-hooks.html" class="sidebar-link">生命周期钩子</a></li><li><a href="/api/options-assets.html" class="sidebar-link">选项/Assets</a></li><li><a href="/api/options-composition.html" class="sidebar-link">组合</a></li><li><a href="/api/options-misc.html" class="sidebar-link">其他</a></li></ul></section></li><li><a href="/api/instance-properties.html" class="sidebar-link">实例 property</a></li><li><a href="/api/instance-methods.html" class="sidebar-link">实例方法</a></li><li><a href="/api/directives.html" class="sidebar-link">指令</a></li><li><a href="/api/special-attributes.html" class="sidebar-link">特殊指令</a></li><li><a href="/api/built-in-components.html" class="sidebar-link">内置组件</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>响应性 API</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/api/basic-reactivity.html" aria-current="page" class="active sidebar-link">响应性基础 API</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#reactive" class="sidebar-link">reactive</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#readonly" class="sidebar-link">readonly</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#isproxy" class="sidebar-link">isProxy</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#isreactive" class="sidebar-link">isReactive</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#isreadonly" class="sidebar-link">isReadonly</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#toraw" class="sidebar-link">toRaw</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#markraw" class="sidebar-link">markRaw</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#shallowreactive" class="sidebar-link">shallowReactive</a></li><li class="sidebar-sub-header"><a href="/api/basic-reactivity.html#shallowreadonly" class="sidebar-link">shallowReadonly</a></li></ul></li><li><a href="/api/refs-api.html" class="sidebar-link">Refs</a></li><li><a href="/api/computed-watch-api.html" class="sidebar-link">Computed 与 watch</a></li></ul></section></li><li><a href="/api/composition-api.html" class="sidebar-link">组合 API</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="响应性基础-api"><a href="#响应性基础-api" class="header-anchor">#</a> 响应性基础 API</h1> <blockquote><p>本节例子中代码使用的<a href="/guide/single-file-component.html">单文件组件</a>语法</p></blockquote> <h2 id="reactive"><a href="#reactive" class="header-anchor">#</a> <code>reactive</code></h2> <p>返回对象的响应式副本</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span> count<span class="token operator">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>响应式转换是“深”的——它影响所有嵌套 property。在基于 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy" target="_blank" rel="noopener noreferrer">ES2015 Proxy<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 的实现中，返回的代理是<strong>不</strong>等于原始对象。建议只使用响应式代理，避免依赖原始对象。</p> <p><strong>类型声明：</strong></p> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token keyword">function</span> <span class="token generic-function"><span class="token function">reactive</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token constant">T</span> <span class="token keyword">extends</span> object<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>target<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">)</span><span class="token operator">:</span> UnwrapNestedRefs<span class="token operator">&lt;</span><span class="token constant">T</span><span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="readonly"><a href="#readonly" class="header-anchor">#</a> <code>readonly</code></h2> <p>获取一个对象 (响应式或纯对象) 或 <a href="/api/refs-api.html#ref">ref</a> 并返回原始代理的只读代理。只读代理是深层的：访问的任何嵌套 property 也是只读的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> original <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span> count<span class="token operator">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> copy <span class="token operator">=</span> <span class="token function">readonly</span><span class="token punctuation">(</span>original<span class="token punctuation">)</span>

<span class="token function">watchEffect</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 适用于响应性追踪</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>copy<span class="token punctuation">.</span>count<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 变更original 会触发侦听器依赖副本</span>
original<span class="token punctuation">.</span>count<span class="token operator">++</span>

<span class="token comment">// 变更副本将失败并导致警告</span>
copy<span class="token punctuation">.</span>count<span class="token operator">++</span> <span class="token comment">// 警告!</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="isproxy"><a href="#isproxy" class="header-anchor">#</a> <code>isProxy</code></h2> <p>检查对象是 <a href="#reactive"><code>reactive</code></a> 还是 <a href="#readonly"><code>readonly</code></a>创建的代理</p> <h2 id="isreactive"><a href="#isreactive" class="header-anchor">#</a> <code>isReactive</code></h2> <p>检查对象是否是 <a href="#reactive"><code>reactive</code></a>创建的响应式代理</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> reactive<span class="token punctuation">,</span> isReactive <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token function">setup</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      name<span class="token operator">:</span> <span class="token string">'John'</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isReactive</span><span class="token punctuation">(</span>state<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// -&gt; true</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>如果代理是 <a href="#readonly"><code>readonly</code></a> 创建的，但还包装了由 <a href="#reactive"><code>reactive</code></a> 创建的另一个代理，它也会返回 <code>true</code>。</p> <div class="language-js line-numbers-mode"><div class="highlight-lines"><br><br><br><br><br><br><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><br><br><br></div><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> reactive<span class="token punctuation">,</span> isReactive<span class="token punctuation">,</span> readonly <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token function">setup</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      name<span class="token operator">:</span> <span class="token string">'John'</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token comment">// 从普通对象创建的只读代理</span>
    <span class="token keyword">const</span> plain <span class="token operator">=</span> <span class="token function">readonly</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      name<span class="token operator">:</span> <span class="token string">'Mary'</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isReactive</span><span class="token punctuation">(</span>plain<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// -&gt; false</span>

    <span class="token comment">// 从响应式代理创建的只读代理</span>
    <span class="token keyword">const</span> stateCopy <span class="token operator">=</span> <span class="token function">readonly</span><span class="token punctuation">(</span>state<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isReactive</span><span class="token punctuation">(</span>stateCopy<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// -&gt; true</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h2 id="isreadonly"><a href="#isreadonly" class="header-anchor">#</a> <code>isReadonly</code></h2> <p>检查对象是否是由<a href="#readonly"><code>readonly</code></a>创建的只读代理。</p> <h2 id="toraw"><a href="#toraw" class="header-anchor">#</a> <code>toRaw</code></h2> <p>返回 <a href="#reactive"><code>reactive</code></a> 或 <a href="#readonly"><code>readonly</code></a> 代理的原始对象。这是一个转义口，可用于临时读取而不会引起代理访问/跟踪开销，也可用于写入而不会触发更改。不建议保留对原始对象的持久引用。请谨慎使用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">const</span> reactiveFoo <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toRaw</span><span class="token punctuation">(</span>reactiveFoo<span class="token punctuation">)</span> <span class="token operator">===</span> foo<span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="markraw"><a href="#markraw" class="header-anchor">#</a> <code>markRaw</code></h2> <p>标记一个对象，使其永远不会转换为代理。返回对象本身。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token function">markRaw</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isReactive</span><span class="token punctuation">(</span><span class="token function">reactive</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// false</span>

<span class="token comment">// 嵌套在其他响应式对象中时也可以使用</span>
<span class="token keyword">const</span> bar <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span> foo <span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isReactive</span><span class="token punctuation">(</span>bar<span class="token punctuation">.</span>foo<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p>下方的 <code>markRaw</code> 和 shallowXXX API 使你可以有选择地选择退出默认的深度响应式/只读转换，并将原始的，非代理的对象嵌入状态图中。它们可以在各种情况下使用：</p> <ul><li>有些值不应被设置为被动的，例如复杂的第三方类实例或 Vue 组件对象。</li></ul> <p>当渲染具有不可变数据源的大列表时，跳过代理转换可以提高性能。</p> <p>它们被认为是高阶的，因为原始选择退出仅在根级别，因此，如果将嵌套的、未标记的原始对象设置为响应式对象，然后再次访问它，则可以得到代理版本。这可能会导致<strong>本源危害</strong>——即执行依赖于对象本身但同时使用同一对象的原始版本和代理版本的操作：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token function">markRaw</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  nested<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> bar <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// 虽然 `foo` 被标记为原始，foo.nested 不是。</span>
  nested<span class="token operator">:</span> foo<span class="token punctuation">.</span>nested
<span class="token punctuation">}</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">.</span>nested <span class="token operator">===</span> bar<span class="token punctuation">.</span>nested<span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>本源危害通常很少见。然而，为了在安全地避免本源危害的同时正确地使用这些 API，需要对响应性系统的工作原理有一个坚实的理解。</p></div> <h2 id="shallowreactive"><a href="#shallowreactive" class="header-anchor">#</a> <code>shallowReactive</code></h2> <p>创建一个响应式代理，该代理跟踪其自身 property 的响应性，但不执行嵌套对象的深度响应式转换 (暴露原始值)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token function">shallowReactive</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  foo<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  nested<span class="token operator">:</span> <span class="token punctuation">{</span>
    bar<span class="token operator">:</span> <span class="token number">2</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 改变状态本身的性质是响应式的</span>
state<span class="token punctuation">.</span>foo<span class="token operator">++</span>
<span class="token comment">// ...但是不转换嵌套对象</span>
<span class="token function">isReactive</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>nested<span class="token punctuation">)</span> <span class="token comment">// false</span>
state<span class="token punctuation">.</span>nested<span class="token punctuation">.</span>bar<span class="token operator">++</span> <span class="token comment">// 非响应式</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="shallowreadonly"><a href="#shallowreadonly" class="header-anchor">#</a> <code>shallowReadonly</code></h2> <p>创建一个代理，使其自身的 property 为只读，但不执行嵌套对象的深度只读转换 (暴露原始值)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token function">shallowReadonly</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  foo<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  nested<span class="token operator">:</span> <span class="token punctuation">{</span>
    bar<span class="token operator">:</span> <span class="token number">2</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 改变状态本身的property将失败</span>
state<span class="token punctuation">.</span>foo<span class="token operator">++</span>
<span class="token comment">// ...但适用于嵌套对象</span>
<span class="token function">isReadonly</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>nested<span class="token punctuation">)</span> <span class="token comment">// false</span>
state<span class="token punctuation">.</span>nested<span class="token punctuation">.</span>bar<span class="token operator">++</span> <span class="token comment">// 适用</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></div> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/api/built-in-components.html" class="prev">内置组件</a></span> <span class="next"><a href="/api/refs-api.html">Refs</a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.0a9bf1a0.js" defer></script><script src="/assets/js/9.f307b0a5.js" defer></script><script src="/assets/js/18.9c9769d1.js" defer></script><script src="/assets/js/10.523605bb.js" defer></script><script src="/assets/js/37.af806c96.js" defer></script>
  </body>
</html>
